//
//  SISSHomeSearchView.m
//  SISSAPP
//
//  Created by 优房网 on 2017/3/17.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSHomeSearchView.h"

@interface SISSHomeSearchView ()

/// chooseCityButton
@property (weak, nonatomic) UIButton *chooseCityButton;

@end
@implementation SISSHomeSearchView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        
        [self setupUI];
    }
    return self;
}

- (void)setCity:(NSString *)city {
    _city = city;
    [self.chooseCityButton setTitle:city forState:UIControlStateNormal];
}

- (void)chooseCityButtonClickAction {
    if ([self.delegate respondsToSelector:@selector(chooseCityButtonClickAction)]) {
        [self.delegate chooseCityButtonClickAction];
    }
}

- (void)qrcodeButtonClickAction {
    if ([self.delegate respondsToSelector:@selector(qrcodeButtonClickAction)]) {
        [self.delegate qrcodeButtonClickAction];
    }
}

- (void)setupUI {
    
    self.backgroundColor = [UIColor clearColor];
    
    UIButton *chooseCityButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [chooseCityButton setTitle:@"深圳" forState:UIControlStateNormal];
    [chooseCityButton setTitleColor:MAIN_TEXT_COLOR forState:UIControlStateNormal];
    [chooseCityButton setBackgroundColor:[UIColor whiteColor]];
    chooseCityButton.titleLabel.font = [UIFont systemFontOfSize:16];
    chooseCityButton.layer.cornerRadius = 10;
    chooseCityButton.layer.masksToBounds = YES;
    [chooseCityButton addTarget:self action:@selector(chooseCityButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:chooseCityButton];
    self.chooseCityButton = chooseCityButton;
    
    UIView *rightView = [[UIView alloc] init];
    rightView.layer.cornerRadius = 10;
    rightView.layer.masksToBounds = YES;
    rightView.backgroundColor = [UIColor whiteColor];
    [self addSubview:rightView];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search"]];
    [imageView sizeToFit];
    [rightView addSubview:imageView];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = COLOR(0x8998A5);
    [rightView addSubview:lineView];
    
    UILabel *label = [[UILabel alloc] init];
    label.text = @"搜索房源、客源或合作";
    label.textColor = COLOR(0x8998A5);
    [rightView addSubview:label];
    
    UIButton *qrcodeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [qrcodeButton setImage:[UIImage imageNamed:@"scan"] forState:UIControlStateNormal];
    [qrcodeButton addTarget:self action:@selector(qrcodeButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    [rightView addSubview:qrcodeButton];
    
    // 自动布局
    CGFloat margin = 8.0;
    CGSize lineSize = CGSizeMake(1, 16);
    
    [chooseCityButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(self);
        make.width.mas_equalTo(8 * margin);
    }];
    
    [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(chooseCityButton.mas_right).offset(0.5 * margin);
        make.top.right.bottom.equalTo(self);
    }];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(rightView);
        make.left.equalTo(rightView).offset(margin);
    }];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(rightView);
        make.left.equalTo(imageView.mas_right).offset(margin);
        make.size.mas_equalTo(lineSize);
    }];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(rightView);
        make.left.equalTo(lineView).offset(margin);
    }];
    
    [qrcodeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.equalTo(rightView).offset(-4);
    }];
}

@end
